package haxeframe.physics.fixtures;

import box2D.dynamics.B2FixtureDef;

import haxeframe.physics.Assets;
import haxeframe.physics.Material;

class Fixture
{
	public var definitions:Array<B2FixtureDef>;
	
	public var material:Material;
	
	public var _xml:Xml;
	public var _scale:Float;
	public var _symbolScale:Float=1;
	
	
	public function new(xml:Xml, scale:Float, symbolScale:Float)
	{
		_xml = xml;
		_scale = scale;
		_symbolScale = symbolScale;
		init();
	}

	public function Fixture()
	{
	}
	
	public function init():Void
	{
 		definitions = new Array<B2FixtureDef>();
		var def:B2FixtureDef = new B2FixtureDef();
		
		if(_xml.exists('material')) material = Assets.getMaterial(_xml.get('material'));

		def.density = (material != null) ? material.density : (_xml.exists('density')) ? Std.parseFloat(_xml.get('density')) : 1;
		def.friction = (material != null) ? material.friction : (_xml.exists('friction')) ? Std.parseFloat(_xml.get('friction')) : 1;
		def.restitution = (material != null) ? material.restitution : (_xml.exists('restitution')) ? Std.parseFloat(_xml.get('restitution')) : 0;
		
		if (_xml.exists('categoryBits')) def.filter.categoryBits = Std.parseInt(_xml.get('categoryBits'));
		if (_xml.exists('groupIndex')) def.filter.groupIndex = Std.parseInt(_xml.get('groupIndex'));
		if (_xml.exists('maskBits')) def.filter.maskBits = Std.parseInt(_xml.get('maskBits'));
		
		if (_xml.get('isSensor') == 'true') def.isSensor = true;
		
		definitions.push(def);
	}
}